﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using JsonTest1.Data;

namespace JsonTest1.Net
{
    public abstract class HttpMessage : IRevocable
    {
        protected IHttpStateListener httpStateListener;

        HttpEngine mHttpGet;
        public HttpMessage()
        {
            init();
        }

        protected void init()
        {
            mHttpGet = new HttpEngine();
            mHttpGet.OnDataStreamGenerated += ((stream, data) =>
            {
                HttpDataStreamCalback(stream, data);
            });

            mHttpGet.ProcessCompleted += (() =>
            {
                this.HttpCompleted();

            });

            mHttpGet.OnGetFailEvent += ((failType, errorType, data) =>
            {
                this.HttpFailCalback(failType, errorType, data);
            });

            mHttpGet.OnProgressChanged += ((progress, totalProgress, type) => {
                this.HttpProgressChangedCalback(progress, totalProgress, type);
            });
        }

        public void BeginRequest(String url)
        {
            mHttpGet.BeginRequest(url);
        }

        public void RevokeAsync()
        {
            mHttpGet.RevokeAsync();
        }

        public  void HttpFailCalback(int failType, int errorType, ResultData data)
        {
            if (httpStateListener != null)
            {
                httpStateListener.HttpFailCalback(failType, errorType,data);
            }
        }
        public  void HttpDataStreamCalback(Stream stream, ResultData data)
        {
            if (HttpGeneratorDataCallback(stream) == false)
            {
                if (httpStateListener != null)
                {
                    httpStateListener.HttpDataStreamCalback(stream, data);
                }
            }

        }

        public abstract bool HttpGeneratorDataCallback(Stream stream);

        public  void HttpProgressChangedCalback(int progress, long totalProgress, HTTP_TYPE type) {
            if (httpStateListener != null)
                httpStateListener.HttpProgressChangedCalback(progress, totalProgress, type);
        }


        public  void HttpCompleted() {
            if (httpStateListener != null)
            {
                httpStateListener.HttpCompleted();
            }
        }

        public void setHttpListener(IHttpStateListener listener)
        {
           httpStateListener = listener;
        }
    }
}
